﻿using CustomPlayerEffects;
using MapGeneration;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using RoleCustom.Main;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using Utils;


namespace RoleCustom.Event
{
    /// <summary>
    /// 
    /// </summary>
    public class SCP096Event
    {
        [PluginEvent(ServerEventType.PlayerSpawn)]
        public void OnPlayerSpawn(PlayerSpawnEvent ev)
        {
            if (ev.Player.Role != PlayerRoles.RoleTypeId.Scp096) return;

            TimeCoroutine.Delay(0.5f, () =>
            {
                PlayerUtils.SetHP(ev.Player,
                    CodeLoad.Singleton.Config.SCP096Event.Health,
                    CodeLoad.Singleton.Config.SCP096Event.Health);

                PlayerUtils.SetAP(ev.Player,
                    CodeLoad.Singleton.Config.SCP096Event.AHealth,
                    CodeLoad.Singleton.Config.SCP096Event.AHealth);

                if (Round.IsLocked) return;

                this.SetSpawnPoint(ev);
                this.SendBroadcast(ev);
            });
        }

        [PluginEvent(ServerEventType.Scp096StartCrying)]
        public void On096StartCrying(Scp096StartCryingEvent ev)
        {
            List<Player> matches;

            m_InfluenceCOR = TimeCoroutine.Loop(-1, 1, () =>
            {
                matches = Player.GetPlayers()
                    .FindAll(e => e.IsAlive && e.Team != PlayerRoles.Team.SCPs && Vector3.Distance(ev.Player.Position, e.Position) <= 30f);

                matches.ForEach(e => e.ReferenceHub.playerEffectsController.ChangeState<Burned>(1, 2, true));
            });
        }

        [PluginEvent(ServerEventType.Scp096TryNotCry)]
        public void On096TryNotCry(Scp096TryNotCryEvent ev)
        {
            TimeCoroutine.Stop(m_InfluenceCOR);
        }



        [Description("")] Coroutine m_InfluenceCOR;


        private void SetSpawnPoint(PlayerSpawnEvent ev)
        {
            bool isGetPoint = SpawnPointUtils.TryGetSpawnPoint(RoomName.Hcz096, new Vector3(-2f, 1f, 0f), out Vector3 position);
            if (isGetPoint)
            {
                Log.Info("SCP-096|更新坐标");
                ev.Player.Position = position;
            }
        }
        private void SendBroadcast(PlayerSpawnEvent ev)
        {
            string message = "嘿！<color=red>SCP-096</color> 配合<color=red>己方阵营</color>清除设施内的所有敌人!";
            MessageStringUtils.GernerateMissionMessage(message, out string output);
            ev.Player.SendBroadcast(output, 15, Broadcast.BroadcastFlags.Truncated, true);

            TimeCoroutine.Delay(10f, () =>
            {
                string nextStep = "嘿！现在你可以（突破）打开收容门了！";
                MessageStringUtils.GernerateMissionMessage(nextStep, out string nextStep2);
                ev.Player.SendBroadcast(nextStep2, 15, Broadcast.BroadcastFlags.Truncated, true);

                TimeCoroutine.Start(IDoCheck());
            });
        }

        private bool GetPos()
        {
            Player scp096 = Player.GetPlayers().Find(e => e.Role == PlayerRoles.RoleTypeId.Scp096);

            if (scp096 == null) return false;

            if (scp096.Room.Name == RoomName.Hcz096) return true;
            return false;
        }


        private IEnumerator IDoCheck()
        {
            WaitForSeconds waitTime = new WaitForSeconds(1f);
            Player match;
            bool isIn = true;

            do
            {
                match = Player.GetPlayers().Find(e => e.IsAlive && e.IsSCP && e.Role == PlayerRoles.RoleTypeId.Scp096);
                if (match != null && match.Room.Name == RoomName.Hcz096)
                {
                    isIn = true;
                    match.IsBypassEnabled = true;
                }
                else
                {
                    isIn = false;
                }

                yield return waitTime;
            }
            while (isIn);

            match.IsBypassEnabled = false;
        }
    }
}
